package com.paipaideli.ui.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.airbnb.epoxy.EpoxyAdapter;
import com.airbnb.epoxy.EpoxyModelWithView;
import com.paipaideli.ui.home.adapter.model.BannerModel;
import com.paipaideli.ui.home.adapter.model.GoodsView;
import com.paipaideli.ui.home.adapter.model.HomeIconsModel;
import com.paipaideli.ui.home.adapter.model.UpMarqueeModel;
import com.paipaideli.ui.home.bean.HomeBean;

import java.util.ArrayList;
import java.util.List;

public class HomePageAdapter extends EpoxyAdapter {

    Context context;

    public HomePageAdapter(Context context) {
        this.context = context;
    }


    public void setDate(HomeBean.Data list) {
        this.models.add(new BannerModelViewHolder(list.bannerRes)); //第一个显示
        if (list.sysDocRes.size() > 0) {
            this.models.add(new IconViewHolder(list.sysDocRes)); //第二个显示
        }
        if (list.auctionInfoRes.size() > 0) {
            this.models.add(new GoodsViewHolder(list.auctionInfoRes)); //第四个显示
        }
    }


    //首页banner
    public class BannerModelViewHolder extends EpoxyModelWithView<BannerModel> {
        List<HomeBean.Data.BannerRes> banners = new ArrayList<>();

        public BannerModelViewHolder(List<HomeBean.Data.BannerRes> values) {
            if (values != null) {
                this.banners = values;
            }
        }

        protected BannerModel buildView(ViewGroup parent) {
            return new BannerModel(HomePageAdapter.this.context);
        }

        public int getSpanSize(int totalSpanCount, int position, int itemCount) {
            return totalSpanCount;
        }

        public void bind(BannerModel view) {
            super.bind(view);
            try {
                if (banners != null && banners.size() > 0 && this.banners != null) {
                    view.clear();
                    view.setBanners(banners);
                    for (HomeBean.Data.BannerRes item : banners) {
                        view.addBanner(item.imagePath, item.title, item.externalLinks);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    //首页滚动展示
//    public class HeadLineModelViewHolder extends EpoxyModelWithView<UpMarqueeModel> {
//        List<HomeBean.Data.Notices> itemView;
//
//        public HeadLineModelViewHolder( List<HomeBean.Data.Notices> itemView) {
//            this.itemView = itemView;
//        }
//
//        public int getSpanSize(int totalSpanCount, int position, int itemCount) {
//            return totalSpanCount;
//        }
//
//        protected UpMarqueeModel buildView(ViewGroup parent) {
//            return new UpMarqueeModel(HomePageAdapter.this.context);
//        }
//
//        public void bind(UpMarqueeModel view) {
//            super.bind(view);
//            view.setDate(itemView);
//        }
//    }

    //首页icon
    public class IconViewHolder extends EpoxyModelWithView<HomeIconsModel> {
        List<HomeBean.Data.SysDocRes> docRes = new ArrayList<>();

        public IconViewHolder(List<HomeBean.Data.SysDocRes> values) {
            if (values != null) {
                this.docRes = values;
            }
        }

        public int getSpanSize(int totalSpanCount, int position, int itemCount) {
            return totalSpanCount;
        }

        protected HomeIconsModel buildView(ViewGroup parent) {
            return new HomeIconsModel(HomePageAdapter.this.context);
        }

        public void bind(HomeIconsModel view) {
            super.bind(view);
            view.setDate(this.docRes);
        }
    }

    //首页拍品数据
    public class GoodsViewHolder extends EpoxyModelWithView<GoodsView> {
        List<HomeBean.Data.AuctionInfoRes> values = new ArrayList<>();

        public GoodsViewHolder(List<HomeBean.Data.AuctionInfoRes> values) {
            if (values != null) {
                this.values = values;
            }
        }

        protected GoodsView buildView(ViewGroup parent) {
            return new GoodsView(context);
        }

        public int getSpanSize(int totalSpanCount, int position, int itemCount) {
            return totalSpanCount;
        }

        public void bind(GoodsView view) {
            super.bind(view);
            try {
                view.setData(values);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clear() {
        this.models.clear();
    }

}
